from spire.xls import *
from spire.xls.common import *

from server import FileParser


class XlsParser(FileParser):

    extensions = ['xls', 'xlsx']

    def parse_file_to_text(self, filename):
        wb = Workbook()
        wb.LoadFromFile(filename)
        sheet = wb.Worksheets[0]
        locatedRange = sheet.AllocatedRange
        rows = []
        for i in range(len(sheet.Rows)):
            # 遍历各列
            cols = []
            for j in range(len(locatedRange.Rows[i].Columns)):
                cols.append(locatedRange[i + 1, j + 1].Value)
            rows.append("\t".join(cols))
        return "\n".join(rows)
